﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SQLDMO;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace DBBackup
{
    public class MSSQLDatabase:IDatabase
    {
        #region Ctor

        public MSSQLDatabase()
        {
            LoginSecure = false;
        }

        #endregion
        public static bool BackUp(string server, string uid, string pwd, string database, string path)
        {

            SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
            SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
            try
            {
                oSQLServer.LoginSecure = false;
                oSQLServer.Connect(server, uid, pwd);
                oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
                oBackup.Database = database;
                oBackup.Files = path;
                oBackup.BackupSetName = database;
                oBackup.BackupSetDescription = "数据库备份";
                oBackup.Initialize = true;
                oBackup.SQLBackup(oSQLServer);
                return true;
            }
            catch (Exception ex)
            {
                return false;
                throw ex;
            }
            finally
            {
                oSQLServer.DisConnect();
            }


        }

        public static void SQLDbRestore(string server, string uid, string pwd, string database, string restorePath)
        {

            SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
            SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
            oRestore.Action = 0;
            //SQLDMO.RestoreSink_PercentCompleteEventHandler pceh = new SQLDMO.RestoreSink_PercentCompleteEventHandler(Step);
            // oRestore.PercentComplete += pceh;
            try
            {
                oSQLServer.Connect(server, uid, pwd);
                SQLDMO.QueryResults qr = oSQLServer.EnumProcesses(-1);
                int iColPIDNum = -1;
                int iColDbName = -1;

                //杀死其它的连接进程
                for (int i = 1; i <= qr.Columns; i++)
                {
                    string strName = qr.get_ColumnName(i);
                    if (strName.ToUpper().Trim() == "SPID")
                    {
                        iColPIDNum = i;
                    }
                    else if (strName.ToUpper().Trim() == "DBNAME")
                    {
                        iColDbName = i;
                    }
                    if (iColPIDNum != -1 && iColDbName != -1)
                        break;
                }

                for (int i = 1; i <= qr.Rows; i++)
                {
                    int lPID = qr.GetColumnLong(i, iColPIDNum);
                    string strDBName = qr.GetColumnString(i, iColDbName);
                    if (strDBName.ToUpper() == "CgRecord".ToUpper())
                        oSQLServer.KillProcess(lPID);
                }
                oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
                oRestore.Database = database;
                oRestore.Files = restorePath;
                oRestore.FileNumber = 1;
                oRestore.ReplaceDatabase = true;
                oRestore.SQLRestore(oSQLServer);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                oSQLServer.DisConnect();
            }


        } 
        
        #region IDatabase Members

        public string ServerName
        {
            get;
            set;
        }

        public string UserName
        {
            get;
            set;
        }

        public string Password
        {
            get;
            set;
        }

        public bool LoginSecure
        {
            get;
            set;
        }

        public bool Restore(string restoreToDbName, string restoreFilePath)
        {
            SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
            SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
            oRestore.Action = 0;
            //SQLDMO.RestoreSink_PercentCompleteEventHandler pceh = new SQLDMO.RestoreSink_PercentCompleteEventHandler(Step);
            // oRestore.PercentComplete += pceh;
            try
            {
                oSQLServer.LoginSecure = LoginSecure;
                if (LoginSecure)
                {
                    oSQLServer.Connect(ServerName);
                }
                else
                {
                    oSQLServer.Connect(ServerName, UserName, Password);
                }
                SQLDMO.QueryResults qr = oSQLServer.EnumProcesses(-1);
                int iColPIDNum = -1;
                int iColDbName = -1;

                //杀死其它的连接进程
                for (int i = 1; i <= qr.Columns; i++)
                {
                    string strName = qr.get_ColumnName(i);
                    if (strName.ToUpper().Trim() == "SPID")
                    {
                        iColPIDNum = i;
                    }
                    else if (strName.ToUpper().Trim() == "DBNAME")
                    {
                        iColDbName = i;
                    }
                    if (iColPIDNum != -1 && iColDbName != -1)
                        break;
                }

                for (int i = 1; i <= qr.Rows; i++)
                {
                    int lPID = qr.GetColumnLong(i, iColPIDNum);
                    string strDBName = qr.GetColumnString(i, iColDbName);
                    if (strDBName.ToUpper() == "CgRecord".ToUpper())
                        oSQLServer.KillProcess(lPID);
                }
                oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
                oRestore.Database = restoreToDbName;
                oRestore.Files = restoreFilePath;
                oRestore.FileNumber = 1;
                oRestore.ReplaceDatabase = true;
                oRestore.SQLRestore(oSQLServer);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                oSQLServer.DisConnect();
            }
            return false; 
        }

        public bool Backup(string toBackDbName, string toBackFilePath)
        {
            SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
            SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
            try
            {
                oSQLServer.LoginSecure = LoginSecure;
                if (LoginSecure)
                {
                    oSQLServer.Connect(ServerName);
                }
                else
                {
                    oSQLServer.Connect(ServerName, UserName, Password);
                }
                oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
                oBackup.Database = toBackDbName;
                oBackup.Files = toBackFilePath;
                oBackup.BackupSetName = toBackDbName;
                oBackup.BackupSetDescription = "数据库备份";
                oBackup.Initialize = true;
                oBackup.SQLBackup(oSQLServer);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message); 
            }
            finally
            {
                oSQLServer.DisConnect();
            }
            return false; 
        }


        #endregion
    }
}
